﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PMCSerialTransfer.Packets
{
    class RecvFileSizePacket: APacket
    {
        public override bool Packet(byte[] Data)
        {
            return Process(Data);
        }

        public override bool EndProcess(bool Ok)
        {
            //UInt32 crc = 0;
            if (Globals.DataPacket[0] != 0)
            {
                Globals.Message = " is not found.";
                Globals.FileStream.Close();
                File.Delete(Globals.FileStream.Name);
            }
            else
            {
                Globals.FileLen = Globals.DataPacket[1] * (2 << 23);
                Globals.FileLen += Globals.DataPacket[2] * (2 << 15);
                Globals.FileLen += Globals.DataPacket[3] * (2 << 7);
                Globals.FileLen += Globals.DataPacket[4];
                Globals.Message = " Bytes: " + Globals.FileLen.ToString() + "\n";
                Globals.ProgressEvent = Globals.ProgressType.Load;
                Globals.FilePtr = 0;
                //Globals.DataPacket = new byte[6];
                //Globals.PacketSize = 6;
                Globals.DataPacket = new byte[0];
                Globals.PacketSize = 4;
                Globals.Create((byte)CommFactory.Command.SndACK, Globals.DataPacket);
                //byte[] buffer = new byte[2];
                //buffer[0] = 6;
                //buffer[1] = (byte)CommFactory.Command.SndACK;
                //crc = Crc32.Compute(buffer);
                //for (int i = 0; i < buffer.Length; i++)
                //    Globals.DataPacket[i] = buffer[i];
                //byte[] crcBuff = BitConverter.GetBytes(crc);

                //for (int i = 0; i < 4; i++)
                //{
                //    if (BitConverter.IsLittleEndian)
                //    {
                //        Globals.DataPacket[2 + i] = crcBuff[3 - i];
                //    }
                //    else
                //    {
                //        Globals.DataPacket[2 + i]= crcBuff[i];
                //    }
                //}
            }

            
            return Ok;
        }
    }
}
